
#include "softmax_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"

namespace optiling
{
    static ge::graphStatus TilingFunc(gert::TilingContext *context)
    {

        SoftmaxTilingData tiling;

        int dim = *context->GetAttrs()->GetInt(0);

        dim = dim >= 0 ? dim : context->GetInputTensor(0)->GetOriginShape().GetDimNum() + dim;
        tiling.set_dim(dim);

        int32_t y_ndarray[20], x_ndarray[20];
        int32_t y_dimensional;

        // 获取原始shape
        auto shape_y = context->GetInputTensor(0)->GetOriginShape();

        y_dimensional = shape_y.GetDimNum();

        // 将维度较小的张量的高位填充为1, 如原shape = [2, 4], 则填充后为 [4, 2, 1], 逆序的
        for (int i = 0; i < y_dimensional; i++)
        {
            y_ndarray[y_dimensional - i - 1] = shape_y.GetDim(i);

            if (i == dim)
                x_ndarray[y_dimensional - i - 1] = 1;
            else
                x_ndarray[y_dimensional - i - 1] = shape_y.GetDim(i);
        }
        tiling.set_y_dimensional(y_dimensional);
        tiling.set_y_ndarray(y_ndarray);
        tiling.set_x_ndarray(x_ndarray);

        // 每个张量的累积形状数组，如 y_ndarray 为 [4, 2, 2]，则 y_sumndarray 为 [1, 4, 8, 16]
        int32_t y_sumndarray[20], x_sumndarray[20];
        y_sumndarray[0] = 1;
        x_sumndarray[0] = 1;
        for (int i = 1; i <= y_dimensional; i++)
        {
            y_sumndarray[i] = y_sumndarray[i - 1] * y_ndarray[i - 1];
            x_sumndarray[i] = x_sumndarray[i - 1] * x_ndarray[i - 1];
        }
        tiling.set_y_sumndarray(y_sumndarray);
        tiling.set_x_sumndarray(x_sumndarray);

        context->SetBlockDim(1);
        tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
        context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

        size_t *currentWorkspace = context->GetWorkspaceSizes(1);
        currentWorkspace[0] = 0;

        return ge::GRAPH_SUCCESS;
    }
}

namespace ge
{
    static ge::graphStatus InferShape(gert::InferShapeContext *context)
    {
        const gert::Shape *x1_shape = context->GetInputShape(0);
        gert::Shape *y_shape = context->GetOutputShape(0);
        *y_shape = *x1_shape;
        return GRAPH_SUCCESS;
    }
}

namespace ops
{
    class Softmax : public OpDef
    {
    public:
        explicit Softmax(const char *name) : OpDef(name)
        {
            this->Input("x")
                .ParamType(REQUIRED)
                .DataType({ge::DT_FLOAT, ge::DT_FLOAT16})
                .Format({ge::FORMAT_ND, ge::FORMAT_ND})
                .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
            this->Output("y")
                .ParamType(REQUIRED)
                .DataType({ge::DT_FLOAT, ge::DT_FLOAT16})
                .Format({ge::FORMAT_ND, ge::FORMAT_ND})
                .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
            this->Attr("dim").AttrType(OPTIONAL).Int(-1);

            this->SetInferShape(ge::InferShape);

            this->AICore()
                .SetTiling(optiling::TilingFunc);
            this->AICore().AddConfig("ascend910b").AddConfig("ascend310b");
        }
    };

    OP_ADD(Softmax);
}
